package dhm.com.dhmshop.updata.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.base.LazyLoadFragment;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GetMyShopinfo;
import dhm.com.dhmshop.entity.GetShopCats;
import dhm.com.dhmshop.framework.module.home.activity.SearchActivity;


/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class ShopTypeFragment extends LazyLoadFragment implements LoginContract.IView {

    Unbinder unbinder;
    @BindView(R.id.recycle_search)
    TagFlowLayout recycle_search;
    @BindView(R.id.itemName)
    TextView itemName;
    @BindView(R.id.ct1)
    TextView ct1;
    private List<GetMyShopinfo.DataBean> allSearch;

    private PressenterImpl pressenter;

    private final GetShopCats.DataBean shopDetail;
    private View rootView;

    public ShopTypeFragment(GetShopCats.DataBean shopDetail) {
        this.shopDetail = shopDetail;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.shop_type_fregment, container, false);
        }
        unbinder = ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    private void initView() {
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
        ct1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(),SearchActivity.class);
                intent.putExtra("type","1");
                intent.putExtra("shop_id",shopDetail.getShopId()+"");
                intent.putExtra("ct1",shopDetail.getCatId()+"");
                intent.putExtra("ct2","");
                getActivity().startActivity(intent);
            }
        });
        itemName.setText(shopDetail.getCatName());
//设置引入流式布局
        recycle_search.setAdapter(new TagAdapter<GetShopCats.DataBean.ChildBean>(shopDetail.getChild()) {
            @Override
            public View getView(FlowLayout parent, int position, GetShopCats.DataBean.ChildBean s) {
                //加入你的布局
                TextView tv = (TextView) View.inflate(getActivity(), R.layout.shoptext_flow, null);
                tv.setText(s.getCatName());
                return tv;
            }
        });
//        点击流式布局跳转到下个界面
        recycle_search.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {

                Intent intent=new Intent(getActivity(),SearchActivity.class);
                intent.putExtra("type","1");
                intent.putExtra("shop_id",shopDetail.getShopId()+"");
                intent.putExtra("ct1",shopDetail.getCatId()+"");
                intent.putExtra("ct2",shopDetail.getChild().get(position).getCatId()+"");
                getActivity().startActivity(intent);

                return true;
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected int setContentView() {
        return 0;
    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetMyShopinfo){
            GetMyShopinfo getMyShopinfo= (GetMyShopinfo) data;
        }
    }
    @Override
    public void fail(String error) {

    }
}
